package cn.lliiooll.mt;

import cn.lliiooll.Mentor;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;

import java.io.File;
import java.io.IOException;

public class MentorFile {

    private static Mentor m = Mentor.m;

    public static File creatFile(String filename){
        File path = new File(m.getDataFolder() + "//playersdata");
        if(!path.exists()){
            try {
                path.mkdirs();
            } catch (Exception e) {
                m.error("发生一个错误,具体错误如下：\n");
                e.printStackTrace();
            }
        }
        File f = new File(path,filename + ".yml");
        if(!f.exists()){
            try {
               f.createNewFile();
            } catch (IOException e) {
                m.error("发生一个I/O错误,具体错误如下：\n");
                e.printStackTrace();
            }
        }
        return f;
    }

    public static  void set(String filename,String path,Object value){
        FileConfiguration f=getFile(filename);
        f.set(path, value);
        try {
            f.save(creatFile(filename));
        } catch (IOException e) {
            m.error("发生一个I/O错误,具体错误如下：\n");
            e.printStackTrace();
        }

    }

    public static Object get(String filename,String path){
        return  getFile(filename).get(path);
    }

    public static FileConfiguration getFile(String filename){
        FileConfiguration y = YamlConfiguration.loadConfiguration(creatFile(filename));
        return y;
    }

    public static boolean hasValue(String filename, String path){
        if( getFile(filename).get(path) != null){
            return true;
        }
        return false;
    }


}
